import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:images_picker/images_picker.dart';

import 'package:scanner_view/scanner_view.dart';

void main() {
  runApp(
    MaterialApp(
      title: 'Demo',
      home: Demo(),
    ),
  );
}

class Demo extends StatelessWidget {
  const Demo({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      child: MyApp(),
    );
  }
}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    // We also handle the message potentially returning null.
    // try {
    //   platformVersion =
    //       await ScannerView.platformVersion ?? 'Unknown platform version';
    // } on PlatformException {
    //   platformVersion = 'Failed to get platform version.';
    // }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    // setState(() {
    //   _platformVersion = platformVersion;
    // });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
          body: Container(
        child: Align(
          alignment: Alignment.center,
          child: TextButton(
            onPressed: () {
              Navigator.push(
                context,
                new MaterialPageRoute(builder: (context) => new DemoView()),
              );
            },
            child: Text('GO TO DEMO'),
          ),
        ),
      )),
    );
  }
}

class DemoView extends StatefulWidget {
  const DemoView({Key? key}) : super(key: key);

  @override
  _DemoViewState createState() => _DemoViewState();
}

class _DemoViewState extends State<DemoView> {
  MethodChannel methodChannel = MethodChannel("com.vv.scanner.scanner_view");
  String? result;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        child: Stack(
          children: [
            FlutterScanView(
              scanResult: (value) {
                if (result == null) {
                  result = value;
                  Fluttertoast.showToast(
                      msg: value, gravity: ToastGravity.CENTER);
                }
              },
            ),
            Positioned(
              right: 100,
              bottom: 100,
              child: GestureDetector(
                // 开启有关闭手电筒
                onTap: () async {
                  await methodChannel.invokeMethod('OPEN_FLASH');
                },
                child: Container(
                  width: 50,
                  height: 50,
                  color: Colors.red,
                  child: Icon(Icons.lightbulb_outline),
                ),
              ),
            ),
            Positioned(
              left: 100,
              bottom: 100,
              child: GestureDetector(
                ///相册
                onTap: () async {
                  result = null;
                  List<Media>? res = await ImagesPicker.pick(
                    count: 1, // 最大可选择数量
                    pickType: PickType.image, // 选择媒体类型，默认图片
                  );
                  await methodChannel.invokeMethod(
                      'SCAN_IMG', {"imgPath": res?.first.path ?? ''});
                },
                child: Container(
                  width: 50,
                  height: 50,
                  color: Colors.red,
                  child: Icon(Icons.image),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
